/*
 * Form.cpp
 *
 *  Created on: 18.2.2013
 *      Author: Travis
 */

#ifdef _WIN32 
#include <windows.h> 
#endif

#include "Form.h"

namespace Gadgets
{

Form::Form(int _width, int _height)
{
	width = _width;
	height = _height;

	background = NULL;

	Pos = glm::vec2(0.0);

	visible = true;

	root = NULL;

	MouseOverEvent.type = SDL_USEREVENT;
	MouseOverEvent.user.data1 = this;
	MouseOverEvent.user.data2 = NULL;

	mouseInside = false;
}

Form::~Form()
{
	if (background != NULL)
		delete (background);

	for (unsigned int i = 0; i < items.size(); i++)
		if (items[i] != NULL)
			delete (items[i]);
}

void Form::draw()
{

	if (visible)
	{

		if (background != NULL)
			if (root != NULL)
				background->draw(root->getPos() + Pos);
			else
				background->draw(Pos);

		for (unsigned int i = 0; i < items.size(); i++)
			if (items[i] != NULL)
				items[i]->draw();
	}
}

void Form::control(SDL_Event *event)
{
	int mx, my;
	if (visible)
	{
		SDL_GetMouseState(&mx, &my);

		MouseOver(event, mx, my);
	}
}

void Form::MouseOver(SDL_Event *event, int x, int y)
{
	glm::vec2 posBuf;

	if (root != NULL)
		posBuf = root->Pos + Pos;
	else
		posBuf = Pos;

	DataTypes::Rectangle2D rec(glm::vec2(posBuf.x, posBuf.y + height),
			glm::vec2(posBuf.x, posBuf.y), glm::vec2(posBuf.x + width, posBuf.y),
			glm::vec2(posBuf.x + width, posBuf.y + height));

	if (rec.pointInRectangle2D(glm::vec2(x, y)))
	{
		MouseOverEvent.user.code = FORM_EVENT_MOUSE_OVER;

		if (!mouseInside)
		{
			SDL_PushEvent(&MouseOverEvent);
			mouseInside = true;
		}

		for (unsigned int i = 0; i < items.size(); i++)
			if (items[i] != NULL)
				items[i]->MouseOver(event, x, y);
	}
	else
		setMouseInside(false);
}

void Form::setRoot(Form *r)
{
	root = r;
}

int Form::getCountOfItems()
{
	return items.size();
}

void Form::setBackground(std::string file)
{
	background = new Gadgets::Picture2D(width, height,file);
}

void Form::addItem(Form *form)
{
	//form->setPos(form->getPos()+Pos);
	form->setRoot(this);

	items.push_back(form);
}

int Form::getHeight()
{
	return height;
}

void Form::setHeight(int height)
{
	this->height = height;

	if (background != NULL)
		background->setHeight(height);
}

glm::vec2 Form::getPos()
{
	if (root != NULL)
		return root->getPos() + Pos;

	return Pos;
}

void Form::setPos(glm::vec2 pos)
{
	Pos = pos;
}

int Form::getWidth()
{
	return width;
}

void Form::setWidth(int width)
{
	this->width = width;

	if (background != NULL)
		background->setWidth(width);
}

bool Form::isVisible() const
{
	return visible;
}

void Form::setVisible(bool visible)
{
	this->visible = visible;

	if (background != NULL)
		background->setVisible(visible);

	if (!visible)
		mouseInside = false;

	for (unsigned int i = 0; i < items.size(); i++)
		if (items[i] != NULL)
			items[i]->setVisible(visible);
}

void Form::deleteItems()
{
	for (unsigned int i = 0; i < items.size(); i++)
		items[i]->setRoot(NULL);

	items.clear();
}

std::vector<Form*> Form::getItems()
{
	return items;
}

void Form::setItems(std::vector<Form*> items)
{
	this->items = items;
}

void Form::setMouseInside(bool s)
{
	mouseInside = s;

	for (unsigned int i = 0; i < items.size(); i++)
		if (items[i] != NULL)
			items[i]->setMouseInside(s);
}

bool Form::getMouseInside()
{
	return mouseInside;
}

void Form::setUserData2(void * data)
{
	MouseOverEvent.user.data2 = data;
}

int Form::getPosX()
{
	return (int) Pos.x;
}

int Form::getPosY()
{
	return (int) Pos.y;
}

void Form::setPos(int x, int y)
{
	setPos(glm::vec2(x, y));
}

} /* namespace Gadgets */
